#!/bin/bash
set -ex

K3S_WAIT_TIME=15
K3S_NODE_NAME=helmtest-node1
K3S_VERSION=v1.30.2-k3s2

cd $(dirname $0)/..

if [[ ! -f 'bin/helm-controller-image.txt' ]]; then
    echo "Run 'make package' first."
    exit 1
fi

setup_k8s(){
    # Drone launches each step in an isolated network. Check and see if there is another bridge network
    # that is not the default bridge network. If so, use that network for the k3s node.
    BRIDGE_NETWORKS=$(docker network ls --filter driver=bridge --format '{{.Name}}' | grep -v bridge)
    if [ -n "$BRIDGE_NETWORKS" ]; then
        K3S_NODE_NETWORK=$(echo "$BRIDGE_NETWORKS" | head -n 1)
        echo "Using network $K3S_NODE_NETWORK"
    else
        K3S_NODE_NETWORK=bridge
    fi
    # Using k3s with embedded helm controller disabled
    docker pull rancher/k3s:$K3S_VERSION
    docker run --detach --privileged --rm --network="$K3S_NODE_NETWORK" -p 6443 --name $K3S_NODE_NAME --hostname $K3S_NODE_NAME rancher/k3s:$K3S_VERSION server --disable-helm-controller --disable=metrics-server,traefik
    K3S_NODE_IP=$(docker inspect $K3S_NODE_NAME --format="{{index .NetworkSettings.Networks \"$K3S_NODE_NETWORK\" \"IPAddress\"}}")
    sleep $K3S_WAIT_TIME
    docker exec $K3S_NODE_NAME sed "s/127.0.0.1/$K3S_NODE_IP/" /etc/rancher/k3s/k3s.yaml > $PWD/kube_config_cluster.yml
}

teardown_k8s(){
    docker rm -f $K3S_NODE_NAME
}

load_helm_image(){
    docker image save $HELM_CONTROLLER_IMAGE | docker exec -i $K3S_NODE_NAME ctr --namespace k8s.io image import -
}

trap teardown_k8s EXIT
setup_k8s
export KUBECONFIG=$PWD/kube_config_cluster.yml
export HELM_CONTROLLER_IMAGE=$(cat bin/helm-controller-image.txt)
load_helm_image
go test -cover -tags=test -v ./test/...
